//
//  BaseWebViewController.m
//  New_APP
//
//  Created by 桑赐相 on 2017/11/16.
//  Copyright © 2017年 桑赐相. All rights reserved.
//

#import "BaseWebViewController.h"

@interface BaseWebViewController ()

@end

@implementation BaseWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}
-(void)initView{
    self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - TopHeight)];
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    self.webView.backgroundColor = ViewBackgroundColor;
    self.webView.scrollView.backgroundColor = ViewBackgroundColor;
    /** 监听加载进度  */
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
    /** 监听网页title */
    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    self.webView.scrollView.delegate = self;
    self.webView.scrollView.autoresizesSubviews = YES;
    
    [self.view addSubview:self.webView];
    
    if (@available(iOS 11.0, *)) {
        UIEdgeInsets insts = UIEdgeInsetsMake(0, 0,0, 0);
        self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.webView.scrollView.contentInset = insts;
        self.webView.scrollView.scrollIndicatorInsets = self.webView.scrollView.contentInset;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [WebViewJavascriptBridge enableLogging];
    self.bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView];
    [self.bridge setWebViewDelegate:self];
    
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    self.progressView.progressTintColor = [UIColor orangeColor];
    self.progressView.trackTintColor = [UIColor clearColor];
    [self.view addSubview:self.progressView];
    self.hidePrgoress = NO;
}


-(void)webViewLoadUrl:(NSString *)url{
    if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"]) {
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    }else{
        NSString *path = [[NSBundle mainBundle]pathForResource:url ofType:@"html"];
        NSURL *u = [NSURL fileURLWithPath:path];
        [self.webView loadFileURL:u allowingReadAccessToURL:u];
    }
}
-(void)addRefresh{
    self.webView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(down_refreshData)];
}
/** 隐藏进度条 */
-(void)setHidePrgoress:(Boolean)hidePrgoress{
    self.progressView.hidden = hidePrgoress;
}
/** 观查webView 加载进度 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqual: @"estimatedProgress"] && object == _webView) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:_webView.estimatedProgress animated:YES];
        if(_webView.estimatedProgress >= 1.0f){
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:NO];
            }];
        }
    }else if([keyPath isEqual: @"title"]){
        if (self.webViewTitle) {
            self.title = self.webView.title;
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [self registerAction];
}
-(void)registerAction{
    WeakSelf(weakSelf);
    [self.bridge registerHandler:@"alertAction" handler:^(id data, WVJBResponseCallback responseCallback) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:data preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alert addAction:cancel];
        [weakSelf presentViewController:alert animated:YES completion:nil];
    }];
}

/** 下拉刷新   */
-(void)down_refreshData{}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView removeObserver:self forKeyPath:@"title"];
    [self.webView setNavigationDelegate:nil];
    [self.webView setUIDelegate:nil];
    [self.webView removeFromSuperview];
    self.webView = nil;
}

@end
